<template>
  <div>
    <el-card class="box-card" style="margin-bottom: 20px">
      <CategorySelect @getAttrList="getAttrList" :show="!isShowTable"></CategorySelect>
    </el-card>
    <el-card class="box-card">
      <div v-show="isShowTable">
        <el-button type="primary" icon="el-icon-plus" style="margin-bottom: 10px" @click="addAttr" :disabled="!attrList.length > 0">添加属性</el-button>
        <el-table :data="attrList" stripe style="width: 100%" border>
          <el-table-column label="序号" width="80" type="index" align="center"> </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="180"> </el-table-column>
          <el-table-column prop="address" label="属性值列表">
            <template slot-scope="{ row }">
              <el-tag v-for="attr in row.attrValueList" :key="attr.id" hit size="small" style="margin-right: 5px">{{ attr.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="180">
            <template slot-scope="{ row }">
              <el-button type="warning" size="small" icon="el-icon-edit" @click="updateAttr(row)">修改</el-button>
              <el-button type="danger" size="small" icon="el-icon-delete">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isShowTable">
        <el-form :inline="true" ref="form" label-width="55px" :model="attrInfo">
          <el-form-item label="属性值">
            <el-input placeholder="请输入属性名" v-model="attrInfo.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" @click="addAttrValue" :disabled="!attrInfo.attrName">添加属性值</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
        <el-table style="width: 100%; margin: 10px 0" border :data="attrInfo.attrValueList">
          <el-table-column type="index" align="center" label="序号" width="80"> </el-table-column>
          <el-table-column label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <el-input v-model="row.valueName" placeholder="请输入属性值" size="mini" v-if="row.flag" @blur="inputBlurAndEnter(row)" @keyup.native.enter="inputBlurAndEnter(row)" :ref="$index"></el-input>
              <span v-else @click="editMode(row, $index)" style="display: block">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-popconfirm :title="`确定删除${row.valueName}？`" @onConfirm="deleteAttrValue($index)">
                <el-button type="danger" icon="el-icon-delete" size="mini" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="save" :disabled="attrInfo.attrValueList.length == 0">保存</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: 'Attrs',
  data() {
    return {
      attrList: [],
      isShowTable: true,
      // 收集表单数据
      attrInfo: {
        attrName: '',
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
      // 三级分类的id
      categoryObj: {},
    }
  },
  methods: {
    // 自定义事件子给父传参
    getAttrList(data, cateForm) {
      this.attrList = data
      this.attrInfo.categoryId = cateForm.category3Id
      this.categoryObj = cateForm
    },
    // 添加属性值
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo?.id,
        valueName: '',
        flag: true,
      })
      // 因为是用v-if进行切换，所以可能会存在该节点未完全渲染成功
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus()
      })
    },
    // 添加属性
    addAttr() {
      this.isShowTable = false
      // 清空数据
      this.attrInfo = {
        attrName: '',
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      }
    },
    // 编辑按钮
    updateAttr(row) {
      this.isShowTable = false
      this.attrInfo = cloneDeep(row)
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, 'flag', false)
      })
    },
    // 输入框失焦和enter键
    inputBlurAndEnter(row) {
      // 判断是否为空
      if (row.valueName.trim() === '') {
        this.$message({ type: 'warning', message: '必须填写属性值' })
        return
      }
      let isRepeat = this.attrInfo.attrValueList.some((item) => {
        if (row !== item) {
          return row.valueName == item.valueName
        }
      })
      if (isRepeat) {
        this.$message({ type: 'warning', message: '已存在该属性值' })
        return
      }
      row.flag = false
    },
    // 切换为编辑模式
    editMode(row, index) {
      row.flag = true
      // 将对应的input自动聚焦
      this.$nextTick(() => {
        this.$refs[index].focus()
      })
    },
    // 删除属性
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1)
    },
    // 保存提交
    async save() {
      const { category1Id, category2Id, category3Id } = this.categoryObj
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter((item) => {
        if (item.valueName != '') {
          delete item.flag
          return true
        }
      })
      try {
        // 保存请求
        await this.$API.Attrs.addNewAttr(this.attrInfo)
        this.$message({ type: 'success', message: '保存成功！' })
        // 再发请求，展现最新数据
        let res = await this.$API.Attrs.reqAttrList(category1Id, category2Id, category3Id)
        this.attrList = res.data
        this.isShowTable = true
      } catch (error) {
        this.$message({ type: 'danger', message: '操作失败！' })
      }
    },
  },
}
</script>

<style>
</style>